package m.jclecai.com.activity;


import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.xrecyclerview.XRecyclerView;
import com.squareup.okhttp.Request;

import java.io.IOException;

import butterknife.BindView;
import m.jclecai.com.MyApplication;
import m.jclecai.com.R;
import m.jclecai.com.adapter.RecomfamousAdapter;
import m.jclecai.com.adapter.RmfamousListAdapter;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.data.ReceivedData;
import m.jclecai.com.fragment.RecommendFragment;
import m.jclecai.com.listener.Callback;
import m.jclecai.com.utils.http.HttpManager;

/**
 * Created by ZD on 2017/7/20.
 * 全部名家解说
 */

public class RmContentListActivity extends BaseActivity {
    @BindView(R.id.xlvList)
    XRecyclerView xlvList;
    RmfamousListAdapter adapter;
    ReceivedData.ReconmentPersonData reconmentPersonData= null;
    RecomfamousAdapter rfAdapter ;
    ReceivedData.ReconmentContentData reconmentContentData;

    @Override
    protected int getContentView() {
        return R.layout.activity_rm_list;
    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 12345679:
                    LinearLayoutManager lManager = new LinearLayoutManager(RmContentListActivity.this);
                    lManager.setOrientation(LinearLayoutManager.VERTICAL);
                    xlvList.setLayoutManager(lManager);
                    rfAdapter = new RecomfamousAdapter(RmContentListActivity.this,reconmentContentData.records);
                    rfAdapter.setmCallback(callback);
                    xlvList.setAdapter(rfAdapter);
                    xlvList.setNoMore(false);
                    xlvList.setLoadingListener(new XRecyclerView.LoadingListener() {
                        @Override
                        public void onRefresh() {
                            sendReq();
                        }

                        @Override
                        public void onLoadMore() {
                            // load more data here
                            xlvList.loadMoreComplete();
                        }
                    });
                    break;
            }
        }
    };

    @Override
    protected void initView() {
        super.initView();
        setTitle("解说列表");
        leftOptionText("");
        sendReq();

    }


    /**
     *  数据请求
     *  推荐列表
     */
    private void sendReq() {
        Constant_Url.HTTP_URL = MyApplication.HTTP_URL_RECOMMEND;
//        showDialog();
        HttpManager.getAsyn(Constant_Url.APP_RECOMMEND_URL, new HttpManager.StringCallback() {

            @Override
            public void onFailure(Request request, IOException e) {
//                dissDialog();
                xlvList.refreshComplete();
                DisPlay("网络异常！！！");
            }

            @Override
            public void onResponse(String response) {
//                dissDialog();
                xlvList.refreshComplete();
                if (!TextUtils.isEmpty(response)) {
                    Gson gson = new Gson();
                    reconmentContentData = gson.fromJson(response, ReceivedData.ReconmentContentData.class);
                    Message msg = new Message();
                    msg.what = 12345679;
                    handler.sendMessage(msg);
                }
            }
        });
    }

        Callback callback = new Callback() {
            @Override
            public void callback(Object o) {
                super.callback(o);
                if (o.equals(RecommendFragment.JUMP.RmFamousActivity)) {
                    startActivity(new Intent(RmContentListActivity.this, RmFamousActivity.class));
                }
            }
        };
    }
